<?php declare(strict_types=1);


namespace App\Api\V2\Controller\Order\Merchant;

use App\Api\Controllers\BaseController;
use App\Models\MerchantStore;
use App\Param\User\UserParams;
use App\Services\Order\MerchantService;
use App\Services\Order\UserService;
use App\Traits\ResponseTrait;
use Illuminate\Http\Request;


class OrderQueryController extends BaseController
{

    use ResponseTrait;

    protected function returnWhere(\stdClass $merchant, array $data): array
    {
//        $merchant = $this->parseToken();
        $storeIds = MerchantStore::query()->where('merchant_id', $merchant->merchant_id)->pluck("store_id")->toArray();
        $where = [
            "page" => empty($data["page"]) ? 1 : (int)$data["page"],//(int)$request->get("page", 1),
            "pagesize" => empty($data["pagesize"]) ? 15 : $data["pagesize"],//(int)$request->get("pagesize", 10),
            "store_id" => empty($data["store_id"]) ? "" : $data["store_id"],//$request->get('store_id', ''),
            "merchant_id" => empty($data["merchant_id"]) ? "" : $data["merchant_id"],//$request->get('merchant_id', ''),
            "pay_status" => empty($data["pay_status"]) ? "" : $data["pay_status"],//$request->get('pay_status', ''),
            "company" => empty($data["company"]) ? "" : $data["company"],//$request->get('company', ''),
            "start_date" => empty($data["start_date"]) ? date('Y-m-d 00:00:00', time()) : $data["start_date"],//$request->get("start_date", date('Y-m-d 00:00:00', time())),
            "end_date" => empty($data["end_date"]) ? date('Y-m-d 23:59:59', time()) : $data["end_date"],//$request->get("end_date", date('Y-m-d 23:59:59', time())),
            "trade_no" => empty($data["trade_no"]) ? "" : $data["trade_no"],//$request->get('trade_no', ''),
            "out_trade_no" => empty($data["out_trade_no"]) ? "" : $data["out_trade_no"],//$request->get('out_trade_no', ''),
            "device_id" => empty($data["device_id"]) ? "" : $data["device_id"],//$request->get('device_id', ''),
            "merchant_all_store_ids" => $storeIds,
            "discount" => empty($data["discount"]) ? 0 : $data["discount"],
            "ways_source" => empty($data["ways_source"]) ? "" : $data["ways_source"],
            "ways_type" => empty($data["ways_type"]) ? "" : $data["ways_type"],
            "sort" => empty($data["sort"]) ? 0 : $data["sort"],
            "auth_code" => empty($data["auth_code"]) ? "" : $data["auth_code"],
            "qwx_no" => empty($data["qwx_no"]) ? "" : $data["qwx_no"],
        ];
        if(empty($data["out_trade_no"])){
            //时间处理
            if (empty($where["start_date"])) {
                $where["start_date"] = date("Y-m-d", time()) . " 00:00:00";
            }
            if (empty($where["end_date"])) {
                $where["end_date"] = date("Y-m-d", time()) . " 23:59:59";
            }
            if (date("Ymd", strtotime($where["end_date"])) == date("Ymd", time()) && date("Ymd", strtotime($where["start_date"])) != date("Ymd", time())) {
                throw new \Exception("当天数据不能和历史数据一起查询");
            }
            if (date("Ym", strtotime($where["start_date"])) != date("Ym", strtotime($where["end_date"]))) {
                throw new \Exception("不能跨月查询");
            }
            if ($where["end_date"] < $where["start_date"]) {
                throw new \Exception("截止时间必须大于开始时间");
            }
        }
        return $where;
    }


    public function orderQuery(Request $request): string
    {
        try {
            $data = $request->all();
            $merchant = $this->parseToken();
            $where = $this->returnWhere($merchant, $data);
            $merchantService = new MerchantService();
            //判断当日查询 还是历史数据查询
            $result = $merchantService->orderQuery($where);
            return $this->successResponse("ok", $result);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

    public function customOrderQuery(Request $request): string
    {
        try {
            $data = $request->all();
            $merchant = $this->parseToken();
            $where = $this->returnWhere($merchant, $data);
            $merchantService = new MerchantService();
            $result = $merchantService->customOrderQuery($where);
            return $this->successResponse("ok", $result);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

    public function orderQueryExport(Request $request): string
    {
        try {
            $data = $request->all();
            $merchant = $this->parseToken();
            $where = $this->returnWhere($merchant, $data);
            $merchantService = new MerchantService();
            $result = $merchantService->orderQueryExport($where);
            return $this->successResponse("ok", $result);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

    public function orderNewQueryExport(Request $request): string
    {
        try {
            $data = $request->all();
            $merchant = $this->parseToken();
            $where = $this->returnWhere($merchant, $data);
            $merchantService = new MerchantService();
            $merchantService->newOrderQueryExport($where,$merchant->merchant_id,$data);
            return $this->successResponse("ok");
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

    /**
     * 获取导出记录列表
     * @param Request $request
     * @return string
     */
    public function getOrderExportList(Request $request)
    {
        $page = $request->get('page', 1);
        $pageSize = $request->get('pageSize', 10);
        $service = new UserService();
        $merchant = $this->parseToken();
        $where = [
            'export_channel'=>2,
            'uid'=>$merchant->merchant_id,
        ];
        $result = $service->getOrderExportListService($page,$pageSize, $where);
        return $this->successResponse("ok", $result);
    }

    /**
     * 修改导出状态
     * @return string
     */
    public function OrderExportSaveExportStatus()
    {
        $params = UserParams::create();
        $UserService = new UserService();
        $result = $UserService->OrderExportSaveExportStatusService($params);
        if ($result)
            return $this->successResponse("ok");
        return $this->errorResponse('修改失败');
    }


}